﻿package net.wangit.jdbc.transaction;

import javax.naming.InitialContext;
import javax.transaction.TransactionManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * WebSphere事务类
 * 
 * @author watson
 * Feb 12, 2010
 */
public class WebSphereTransaction extends JTATransaction {

	private static final Log log = LogFactory
			.getLog(net.wangit.jdbc.transaction.WebSphereTransaction.class);
	private int version;

	public WebSphereTransaction() {
	}

	public TransactionManager getTransactionManager(InitialContext context)
			throws TransactionException {
		Class clazz = null;
		try {
			clazz = Class
					.forName("com.ibm.ws.Transaction.TransactionManagerFactory");
			version = 5;
			log.info("WebSphere 5.1");
		} catch (Exception e) {
			try {
				clazz = Class
						.forName("com.ibm.ejs.jts.jta.TransactionManagerFactory");
				version = 5;
				log.info("WebSphere 5.0");
			} catch (Exception e2) {
				try {
					clazz = Class.forName("com.ibm.ejs.jts.jta.JTSXA");
				} catch (ClassNotFoundException e1) {
				}
				version = 4;
				log.info("WebSphere 4");
			}
		}
		try {
			return (TransactionManager) clazz.getMethod(
					"getTransactionManager", null).invoke(null, null);
		} catch (Exception e) {
			throw new TransactionException(
					"Could not obtain WebSphere JTSXA instance", e);
		}
	}

	public String getUserTransactionName() {
		return version != 5 ? "jta/usertransaction"
				: "java:comp/UserTransaction";
	}

}
